<template>
  <div class="page">
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="matterModelClick">新增</el-button>
    <div style="margin-top: 10px;">
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        :header-cell-style="{background:'#F2F2F2',color:'#333333'}"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60px"
        />
        <el-table-column
          prop="item_name"
          label="事项名称"
        />
        <el-table-column
          prop="automatic_distribution"
          label="是否自动派发"
        />
        <el-table-column
          prop="submission_time"
          label="提交时间"
        />
        <el-table-column
          align="right"
          label="操作"
        >
          <template slot-scope="scope">
            <span class="tem_btn" :style="red" @click="deleteRow(scope.row.id)">删除</span>
            <span class="tem_btn" :style="blue" @click="editHandleRow(scope.row)">编辑</span>
            <span class="tem_btn" :style="blue" @click="seeHandleRow(scope.row)">查看</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :page-size.sync="page.pageSize"
      :total="page.total"
      :current-page.sync="page.pageIndex"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--新增弹框-->
    <new-issue-modal @addFun="getMatterData" :matterModelShow="matterModelShow" />
<!--    <edit-issue-modal :type="type" :matterEditModelShow="matterEditModelShow" :editId="editId" @editFun="getEditMatterData" />-->
    <div class="container" v-if="matterEditModelShow">
      <el-dialog :title="title" :visible.sync="matterEditModelShow" width="600px" :before-close="handleClose" >
      <div class="container_examine">
        <div class="examine">
          <el-form ref="formInline" :rules="rules" label-position="top" :inline="true" :model="formInline">
            <el-form-item label="事项唯一标识码:" prop="ldentificationCode" style="width: 98%">
              <el-select
                v-model="formInline.ldentificationCode"
                :disabled="disabled"
                filterable
                allow-create
                default-first-option
                placeholder="请输入事项唯一标识码"
                @change="changeHandleCode"
              >
                <el-option
                  v-for="item in matterListCode"
                  :key="item.ldentificationCode"
                  :label="item.ldentificationCode"
                  :value="item.ldentificationCode"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="事项名称:" prop="matterName" style="width: 98%">
              <el-select
                v-model="formInline.matterName"
                :disabled="disabled"
                filterable
                allow-create
                default-first-option
                placeholder="请选择事项名称"
                @change="changeHandle"
              >
                <el-option
                  v-for="item in matterList"
                  :key="item.id"
                  :label="item.item_name"
                  :value="item.item_name"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="是否自动派发:" prop="isSell" style="width: 100%" class="isSell_form">
              <el-radio-group v-model="formInline.isSell" :disabled="disabled">
                <el-radio label="是" />
                <el-radio label="否" />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="派发人员:" prop="isSell" style="width: 100%" class="isSell_form" v-if="formInline.isSell == '是'">
              <el-radio-group v-model="formInline.sellPeople" :disabled="disabled">
                <el-radio label="部门" />
                <el-radio label="乡镇" />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注:" prop="remark"  style="width: 100%">
              <el-input v-model="formInline.remark" :disabled="disabled" placeholder="" />
            </el-form-item>
            <el-form-item label="自动派发浙政钉账号:" v-if="formInline.sellPeople == '部门' && formInline.isSell == '是'">
              <!--<el-input v-model="formInline.number" :disabled="disabled" placeholder="请输入浙政钉账号" />-->
              <el-cascader
                style="width: 100%;"
                v-model="formInline.number"
                :options="unitList"
                :disabled="disabled"
                ref="myCascader"
                :props="{label:'name',value:'code',children:'children'}"
                @change="handleChange"></el-cascader>
            </el-form-item>
            <div v-if="formInline.sellPeople == '乡镇' && this.townnameList.length >0 && formInline.isSell == '是'">
              <el-form-item :label="item.organizationName" v-for="item in townnameList" :key="item.id">
                <el-cascader
                  v-model="item.townOne"
                  :options="item.list"
                  :disabled="disabled"
                  ref="myCascader"
                  :props="{label:'name',value:'code'}"></el-cascader>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </div>
      <div v-if="!disabled" slot="footer" class="dialog-footer">
        <el-button size="small" @click="cancleBtn('formInline')">取消</el-button>
        <el-button size="small" type="primary" @click="saveBtn('formInline')">确认</el-button>
      </div>
    </el-dialog>
    </div>
  </div>
</template>

<script>
// 新增弹框
import NewIssueModal from '../components/NewIssueModal'
import EditIssueModal from '../components/EditIssueModal'
import { getMatterList, deleteRowList } from '../api'
import { dateFormat } from '@/views/assistance/utils'
import {
  getIdentificationCode,
  saveEditMatterList,
  getStructureList,
  getTownshipList,
  getTownnameList,
  getIssueListDetail,
  getMatterDataList
} from '../api'
export default {
  name: 'Issue',
  components: {
    NewIssueModal,
    EditIssueModal
  },
  data() {
    return {
      red: 'color: #FF2929',
      orange: 'color: #2AC23D',
      green: 'color: #542EFF',
      yellow: 'color:#FF8504',
      blue: 'color:#1182FB',
      tableData: [],
      page: {
        pageSize: 12,
        pageIndex: 1,
        total: 0
      },
      loading: false,
      type: '',
      rowList: {},
      matterModelShow:false,
      matterEditModelShow:false,
      detailLists:{},
      editId:'',
      formInline: {
        matterName: '',
        isSell: '是',
        number: [],
        time: '',
        id: '',
        sellPeople:'',
        // departMent: '',
        ldentificationCode: '',
        remark: '',
      },
      rules: {
        matterName: [
          { required: true, message: '请输入事项名称', trigger: 'blur' }
        ],
        isSell: [
          { required: true, message: '是否自动派发', trigger: 'change' }
        ],
        number: [
          { required: true, message: '请输入办事人联系电话', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ],
        ldentificationCode: [
          { required: true, message: '请选择事项唯一标识码', trigger: 'blur' }
        ]
      },
      disabled: false,
      title: '',
      unitList:[],
      townshipList:[],
      selectPeopleList:[],
      matterList:[],
      matterListCode:[],
      townnameList:[],
      newList:[],
      flag:false,
      id:''
    }
  },
  computed: {
  },
  created() {
    this.getDataList()

  },
  methods: {
    // 时间格式化
    formatTime(time) {
      return time ? dateFormat(new Date(time), 'yyyy-MM-dd') : ''
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.pageIndex = val
      this.getDataList()
    },
    // 审核
    matterModelClick() {
      this.type = 'add';
      this.matterModelShow = true;
  },
    editHandleRow(row) {
      this.id = row.id;
      this.disabled = false;
      this.matterEditModelShow= true;
      this.getTownship();
      this.getStructureList();
    },
    seeHandleRow(row) {
      this.id = row.id;
      this.disabled = true;
      this.matterEditModelShow= true;
      this.getTownship();
      this.getStructureList();
    },
    // 获取乡镇
    getTownship(){
      getTownshipList().then(res => {
        console.log(res)
        this.townshipList = res.data.data.sjlist;
        this.getTownname();
      })
    },
    // 获取乡镇名称
    getTownname(){
      getTownnameList().then(res => {
        let that = this;
        res.data.data.map((item,index)=>{
          this.townshipList.map((data,i)=>{
            if(item.organizationCode == data.code){
              res.data.data[index].list = data.children;
            }
          })
        })
        this.getIssueList(that.id);
        this.townnameList = res.data.data;
      })
    },
    //编辑提交
    saveBtn() {
      let that = this;
      if(this.formInline.isSell === "是"){
        if(this.formInline.sellPeople === '乡镇'){
          this.townnameList.map(function(item,index){
            let list = {
              name:item.organizationCode,
              township:item.townOne[0],
              code:item.townOne[1],
            }
            that.selectPeopleList.push(list)
          })
          this.formInline.number = ''
        }else{
          this.formInline.number = this.formInline.number[this.formInline.number.length - 2]+','+this.formInline.number[this.formInline.number.length - 1]
        }
      }else{
        this.formInline.number = ''
      }
      const data = {
        automatic_distribution: this.formInline.isSell,
        id: this.formInline.id,
        distribution_account: this.formInline.number,
        array:this.selectPeopleList,
        item_name: this.formInline.matterName,
        // department: this.formInline.departMent,
        ldentificationCode: this.formInline.ldentificationCode,
        remark: this.formInline.remark,
        type: this.formInline.sellPeople
        // submission_time: this.formatTime(this.formInline.time)
      }
      // return;
      saveEditMatterList(data).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.matterEditModelShow= false;
        }
      })
    },
    //查看详情
    getIssueList(){
      if (this.id != undefined){
        getIssueListDetail(this.id).then(res => {
          console.log(res)
          if(res.data.code == 200){
            let that = this;
            this.formInline.matterName = res.data.data.item_name;
            this.formInline.isSell = res.data.data.automatic_distribution;
            this.formInline.id = res.data.data.id;
            this.formInline.sellPeople = res.data.data.type;
            this.formInline.ldentificationCode = res.data.data.ldentificationCode;
            this.formInline.remark = res.data.data.remark;
            if(res.data.data.type == '部门'){
              this.formInline.number = res.data.data.map;
            }else if(res.data.data.type == '乡镇'){
              if(this.townnameList.length > 0){
                this.townnameList.map(function(item,index){
                  res.data.data.map.map(function(data){
                    if(data.children[0] == item.organizationCode){
                      that.townnameList[index].townOne = (data.children).splice(1,2);
                    }
                  })
                })
                // console.log(this.townnameList)
              }
            }
          }
        })
      } else{
        return;
      }
    },
    // getEditMatterData(data) {
    //   if(data){
    //     this.getDataList()
    //   }
    //   this.matterEditModelShow= false;
    // },
    getStructureList(){
      getStructureList('事项').then(res => {
        this.unitList = res.data.data.sjlist;
      })
    },
    //判断此事项已存在
    changeHandle() {
      this.matterList.map((item) => {
        if (item.item_name === this.formInline.matterName) {
          this.$message({
            type: 'warning',
            message: '此事项已存在'
          })
          this.formInline.matterName = ''
        }
      })
    },
    //判断此事项已存在
    changeHandleCode(){
      this.matterListCode.map((item) => {
        if (item.ldentificationCode === this.formInline.ldentificationCode) {
          this.$message({
            type: 'warning',
            message: '此事项已存在'
          })
          this.formInline.ldentificationCode = ''
        }
      })
    },
    // 获取列表信息
    getDataList() {
      const data = {
        page: this.page.pageIndex,
        limit: this.page.pageSize
      }
      this.loading = true
      getMatterList(data).then(res => {
        this.loading = false
        this.tableData = res.data.data.list
        this.page.total = res.data.data.totalRow
        this.tableData.map((item, index) => {
          this.tableData[index].submission_time = this.formatTime(item.submission_time)
        })
      })
    },
    getMatterData(data) {
      this.matterModelShow = false;
      if(data){
        this.getDataList()
      }
    },
    deleteRow(id) {
      this.$confirm('确认删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.cancelButtonLoading = true
            instance.confirmButtonText = '删除中，请耐心等待...'
            deleteRowList(id)
              .then((res) => {
                if (res.data.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '删除成功!'
                  })

                  this.getDataList()
                } else {
                  this.$message({
                    type: 'error',
                    message: res.data
                  })
                }
              })
              .finally(() => {
                instance.confirmButtonLoading = false
                instance.cancelButtonLoading = false
                done()
              })
          } else {
            done()
          }
        }
      }).catch(() => {})
    },
    handleChange(value) {
      let labelName = this.$refs['myCascader'].getCheckedNodes()[0].pathLabels;
      this.formInline.department = labelName[labelName.length-1];
      this.formInline.number = value;
    },
    cancleBtn(formName) {
      this.matterEditModelShow= false;
    },
    handleClose(){
      this.matterEditModelShow= false;
    }
  }
}
</script>

<style lang="scss" scoped>
  .page{
    position: relative;
    height: calc(100vh - 110px);
    padding: 20px 20px 45px 20px;
    .info_button {
      color: #4885F4;
      cursor: pointer;
    }
    .sell_name{
      padding-right: 10px;
    }
    .tem_btn{
      padding: 0 6px;
      cursor: pointer;
    }
  }

</style>
<style lang="scss" scoped>
.container {
  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
  }
  ::v-deep .el-dialog {
    max-height: 82vh;
    overflow: auto;
  }
  ::v-deep .el-dialog__body {
    margin-top: 25px;
    padding:30px 20px 30px 30px;
  }
  ::v-deep .el-dialog__header {
    background: #fff;
    position: fixed;
    width: 550px;
    z-index: 99;
    /*.el-dialog__title {*/
    /*color: #ffffff;*/
    /*}*/
    .el-dialog__headerbtn {
      top: 14px !important;
      cursor: pointer;
      /*i {*/
      /*color: #ffffff;*/
      /*}*/
    }
  }
  .container_examine{
    margin-top: 15px;
    p{
      font-size: 15px;
      color: #3D4D60;
      font-weight: bold;
    }
    .el-form--inline .el-form-item{
      width: 47%;
      /*margin-bottom:12px;*/
    }
    ::v-deep .el-textarea__inner,
    ::v-deep .el-select,
    ::v-deep .el-cascader,
    ::v-deep .el-date-editor{
      width: 100%;
    }
    ::v-deep .el-cascader{
      //margin-top: 5px;
      /*width: 48%;*/
      //margin-left: 5px;
    }
    ::v-deep .el-form-item__label{
      font-weight: normal;
      line-height:10px;
    }
    .isSell_form{
      ::v-deep .el-form-item__content{
        display: inline-block!important;
        vertical-align: bottom;
        padding-left: 20px;
      }
    }
  }
}

</style>
